<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        /* 图片区域 */
        
        .preview_img {
            position: absolute;
            width: 398px;
            height: 398px;
            border: 1px solid #ccc;
        }
        
        .mask {
            position: absolute;
            top: 0;
            left: 0;
            width: 200px;
            height: 200px;
            background-color: burlywood;
            opacity: 0.5;
        }
        
        .big {
            position: absolute;
            top: 0;
            left: 500px;
            border: 1px solid #ccc;
            width: 500px;
            height: 600px;
            overflow: hidden;
            display: none;
        }
        
        .bigIMg {
            position: absolute;
        }
    </style>
</head>

<body>
    <!-- 图片预览区域 -->
    <div class="preview_img">
        <!-- 图片 -->
        <img src="images/phone.png" alt="">
        <!-- 放大镜 -->
        <div class="mask"></div>
        <!-- 展示放大后图片效果 -->
        <div class="big">
            <img src="images/bigphone.png" alt="" class="bigIMg">
        </div>
    </div>
    <script>
        var di1 = document.querySelector('.preview_img');
        var big = document.querySelector('.big');
        var mask = document.querySelector('.mask');
        var img = document.querySelector('.bigIMg');

        di1.addEventListener('mouseover', function() {
            mask.style.display = 'block';
            big.style.display = 'block';
        })
        di1.addEventListener('mouseout', function() {
            mask.style.display = 'none';
            big.style.display = 'none';
        })
        di1.addEventListener('mousemove', function(e) {
            var x = e.pageX - this.offsetLeft;
            var y = e.pageY - this.offsetTop;
            var maskx = x - mask.offsetWidth / 2;
            var masky = y - mask.offsetHeight / 2;
            if (maskx <= 0) {
                maskx = 0;
            }
            if (maskx >= di1.offsetWidth - mask.offsetWidth) {
                maskx = di1.offsetWidth - mask.offsetWidth;
            }
            if (masky <= 0) {
                masky = 0;
            }
            if (masky >= di1.offsetHeight - mask.offsetHeight) {
                masky = di1.offsetHeight - mask.offsetHeight;
            }
            mask.style.left = maskx + 'px';
            mask.style.top = masky + 'px';
            img.style.left = -maskx * (img.offsetWidth - big.offsetWidth) / (di1.offsetWidth - mask.offsetWidth) + 'px';
            img.style.top = -masky * (img.offsetHeight - big.offsetHeight) / (di1.offsetHeight - mask.offsetHeight) + 'px';
        })
    </script>

</body>

</html>